package cnpm.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import cnpm.dao.BrandBankDAO;
import cnpm.entity.Bank;
import cnpm.entity.BrandBank;
import cnpm.util.HibernateUtil;

public class BrandBankDAOImpl implements BrandBankDAO{

	private SessionFactory sessionFactory;
	private Session session;
	
	public BrandBankDAOImpl() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BrandBank> getAllBrandBank(Bank bank) {
		List<BrandBank> list=null;
		try{
			session = sessionFactory.openSession();
			list = (List<BrandBank>) session.createCriteria(BrandBank.class)
					.add(Restrictions.eq("brandBank", bank))
					.list();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			session.close();
		}
		return list;
	}

	@Override
	public BrandBank getBrandBak(int brandID) {
		BrandBank bank = null;
		try{
			session = sessionFactory.openSession();
			bank = (BrandBank) session.get(BrandBank.class, brandID);
			return bank;
		}catch(HibernateException e1){
			e1.printStackTrace();
		}finally{
			session.close();
		}
		return bank;
	}

}
